package com.lswms.modules.lswms.service.impl.recipient;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.dao.recipient.RecipientsinfoDao;
import com.lswms.modules.lswms.dto.recipient.RecipientsinfoDTO;
import com.lswms.modules.lswms.entity.recipient.RecipientsinfoEntity;
import com.lswms.modules.lswms.service.recipient.RecipientsinfoService;



/**
 * ${comments}
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-07-18
 */
@Service
public class RecipientsinfoServiceImpl extends CrudServiceImpl<RecipientsinfoDao, RecipientsinfoEntity, RecipientsinfoDTO> implements RecipientsinfoService {

    @Override
    public QueryWrapper<RecipientsinfoEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String cardid = (String)params.get("cardid");
        String recipient = (String)params.get("recipient");
        String recipientid = (String)params.get("recipientid");

        QueryWrapper<RecipientsinfoEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(cardid), "cardid", cardid);
        wrapper.eq(StringUtils.isNotBlank(recipientid), "recipientid", recipientid);
        wrapper.like(StringUtils.isNotBlank(recipient), "recipient", recipient);

        return wrapper;
    }

	@Override
	public RecipientsinfoEntity getByCardid(String cardid) {
		QueryWrapper<RecipientsinfoEntity> query = new QueryWrapper<RecipientsinfoEntity>();
		query.eq("cardid", cardid) ;
		
		return baseDao.selectOne(query);
	}

	@Override
	public RecipientsinfoEntity getByParams(Map<String, Object> params) {
		 String cardid = (String)params.get("cardid");
	        String recipientid = (String)params.get("recipientid");
	        
	        if(StringUtils.isAllBlank(cardid,recipientid) ) {
	        	return null;
	        }

	        QueryWrapper<RecipientsinfoEntity> wrapper = new QueryWrapper<>();
	        wrapper.eq(StringUtils.isNotBlank(cardid), "cardid", cardid);
	        wrapper.like(StringUtils.isNotBlank(recipientid), "recipientid", recipientid);
	        wrapper.orderByDesc("addtime");
	        
	      List<RecipientsinfoEntity> list =  baseDao.selectList(wrapper);
	        if(list.size()>0) {
	        	return list.get(0);
	        }
	        
		return null;
	}


}